package com.hzit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzit.entity.Classes;
import com.hzit.entity.PageResult;
import com.hzit.entity.R;
import com.hzit.entity.Student;
import com.hzit.service.IClassesService;
import com.hzit.service.IStudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbxfwf
 * @since 2023-05-25
 */
@RestController
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private IStudentService studentService;
	@Autowired
	private IClassesService classesService;
	//1. 查询所有学生
	@GetMapping("/list")
	public R list(){
		List<Student> list = studentService.list();
		for (Student student : list) {
			Classes classes = classesService.getById(student.getCid());
			student.setCname(classes.getCname());
		}
		return R.ok().data("list",list);
	}
	//2. 分页带条件查询
	@PostMapping("/search/{page}/{pageSize}")
	public R search(@PathVariable int page,@PathVariable int pageSize,@RequestBody(required = false) Student student){
		//2.1 开始分页查询对象
		PageResult<Student> pr = studentService.search(page, pageSize, student);
		//2.2 返回
		return R.ok().data("pr",pr);
	}

}
